<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>复制迭代器中的元素到数组</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.iterator-count.html">« iterator_count</a></li>
      <li style="float: right;"><a href="function.spl-autoload-call.html">spl_autoload_call »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.spl.html">SPL 函数</a></li>
    <li>复制迭代器中的元素到数组</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.iterator-to-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">iterator_to_array</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">iterator_to_array</span> &mdash; <span class="dc-title">复制迭代器中的元素到数组</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.iterator-to-array-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>iterator_to_array</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.traversable.html" class="type Traversable">Traversable</a></span>|<span class="type">array</span></span> <code class="parameter">$iterator</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserve_keys</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   复制迭代器中的元素到数组。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.iterator-to-array-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">iterator</code></dt>

     <dd>

      <p class="para">
       被复制的迭代器。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">preserve_keys</code></dt>

     <dd>

      <p class="para">
       是否使用迭代器元素键作为索引。
      </p>
      <p class="para">
       如果键是 <span class="type">array</span> 或 <span class="type">object</span>，将会生成警告。<strong><code>null</code></strong>
       键将会转换为空字符串，<span class="type">float</span> 键将截断为对应的 <span class="type">int</span>，<span class="type">resource</span>
       键将生成警告并转换为它们的资源 ID，<span class="type">bool</span> 键将转换为整数。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        如果此参数未设置或为 <strong><code>true</code></strong>，则覆盖重复的键。指定键的最后一个值将在返回的 <span class="type">array</span>
        中。设置此参数为 <strong><code>false</code></strong> 以获得任何情况下的所有值。
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.iterator-to-array-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   一个数组，包含迭代器中的元素。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.iterator-to-array-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.2.0</td>
      <td>
       <code class="parameter">iterator</code> 的类型从 <span class="type"><a href="class.traversable.html" class="type Traversable">Traversable</a></span>
       扩展为 <span class="type"><span class="type"><a href="class.traversable.html" class="type Traversable">Traversable</a></span>|<span class="type">array</span></span>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.iterator-to-array-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3667">
    <p><strong>示例 #1 <span class="function"><strong>iterator_to_array()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$iterator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'recipe'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'pancakes'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'egg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'milk'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'flour'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">iterator_to_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">iterator_to_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [&quot;recipe&quot;]=&gt;
  string(8) &quot;pancakes&quot;
  [0]=&gt;
  string(3) &quot;egg&quot;
  [1]=&gt;
  string(4) &quot;milk&quot;
  [2]=&gt;
  string(5) &quot;flour&quot;
}
array(4) {
  [0]=&gt;
  string(8) &quot;pancakes&quot;
  [1]=&gt;
  string(3) &quot;egg&quot;
  [2]=&gt;
  string(4) &quot;milk&quot;
  [3]=&gt;
  string(5) &quot;flour&quot;
}
</pre></div> 
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>